package com.shenmazong.servereduschool.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shenmazong.servereduschool.pojo.TbCollage;
import com.shenmazong.servereduschool.pojo.TbMajor;
import com.shenmazong.servereduschool.service.TbMajorService;
import com.shenmazong.servereduschool.mapper.TbMajorMapper;
import com.shenmazong.utils.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class TbMajorServiceImpl extends ServiceImpl<TbMajorMapper, TbMajor>
implements TbMajorService{
    @Autowired
    TbMajorMapper tbMajorMapper;


    @Override
    public PageInfo findMajorList(Integer pageNum, Integer pageSize, Object tbMajor) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbMajor> tbMajors=tbMajorMapper.findMajorList(tbMajor);
        PageInfo<TbMajor> pageInfo = new PageInfo<>(tbMajors);
        return pageInfo;
    }

    @Override
    public ResultResponse add(TbMajor tbMajor) {
        try {
            tbMajor.setCreateTime(new Date());
            tbMajor.setUpdateTime(new Date());
            int i = tbMajorMapper.insert(tbMajor);
            if(i<1){
                return ResultResponse.FAILED(-1,"添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultResponse.SUCCESS("添加成功");
    }

    @Override
    public ResultResponse queryByMajorId(TbMajor TbMajor) {
        try {
            TbMajor tbMajor = tbMajorMapper.selectById(TbMajor.getMajorId());
            return ResultResponse.SUCCESS(tbMajor);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }

    }

    @Override
    public ResultResponse updateTbMajor(TbMajor tbMajor) {
        try {
            tbMajor.setUpdateTime(new Date());
            int i = tbMajorMapper.updateById(tbMajor);
            if(i<1){
                return ResultResponse.FAILED(-1,"修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultResponse.SUCCESS("修改成功");
    }

    @Override
    public ResultResponse delByMajorId(TbMajor tbMajor) {
        try {
            tbMajorMapper.deleteById(tbMajor.getMajorId());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse delAll(List<TbMajor> ids) {
        if(ids.size()<=0){
            return ResultResponse.FAILED(-1,"至少选择一个");
        }
        for (TbMajor tbMajor : ids) {
            tbMajorMapper.deleteById(tbMajor.getMajorId());
        }
        return ResultResponse.SUCCESS("删除成功");
    }

    @Override
    public ResultResponse findMajor() {
        List<TbMajor> majors = tbMajorMapper.selectList(null);
        return ResultResponse.SUCCESS(majors);
    }
}




